<?php
/**
 * @package: Asalsuri
 * @subpackage: User Model
 * @author: Nguyen Thanh Tung
 * @date: January 24, 2014 - January 27, 2014
 * 
*/
class User_Model extends CI_Model{
	var $database;
	public function __construct() {
        parent::__construct();
		$this->database = $this->db->conn_id;	
	}
	/**
     * @author: Nguyen Thanh Tung
     * @description: GetId
     * @param string $strEmail
     * @return array
    */
	public function get($intUserId) {	
		$stmt = $this->database->prepare('CALL users_get(?)');
		$stmt->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
		$stmt->execute();
		$arrResult 	= $stmt->fetch();			
		return isset($arrResult) ? $arrResult : null;		
	}

	/**
     * @author: Nguyen Thanh Tung
     * @description: Get information by email
     * @param string $strEmail
     * @return array
    */
	public function getEmail($strEmail) {			
		$stmt = $this->database->prepare('CALL users_get_email(?)');
		$stmt->bindParam(1, $strEmail, PDO::PARAM_STR, 150);
		$stmt->execute();
		$arrResult 	= $stmt->fetch();		
		return (isset($arrResult) && is_array($arrResult)) ? $arrResult : null;		
	}
	/**
     * @author: Nguyen Thanh Tung
     * @description: Get information by username
     * @param string $strUsername
     * @return array
    */
	public function getInfoUsername($strUsername) {	
		$stmt = $this->database->prepare('CALL users_get_username(?)');
		$stmt->bindParam(1, $strUsername, PDO::PARAM_STR, 50);
		$stmt->execute();
		$arrResult 	= $stmt->fetch();			
		return isset($arrResult) ? $arrResult : null;		
	}

    /**
    * @author: Nguyen Thanh Tung
    * @description: get all user
    * @param integer $intLoginId: admin role loggin
    * @param integer $intOffset: offset start paging
    * @param integer $intLimit: display row on paging
    * @param integer $intTotal: assign found rows 
    * @param array
    */
    public function getAll($intLoginId, $intStatus, $intPoption, $strKeyword, $strStart, $strEnd, 
																		$intOffset, $intLimit, &$intTotal=0) {
		$stmt = $this->database->prepare('CALL users_getall_a(?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginId,  PDO::PARAM_INT);
		$stmt->bindParam(2, $intStatus,   PDO::PARAM_INT, 4);
		$stmt->bindParam(3, $intPoption,  PDO::PARAM_INT, 4);		
		$stmt->bindParam(4, $strKeyword,  PDO::PARAM_STR, 50);		
		$stmt->bindParam(5, $strDateStart,PDO::PARAM_STR);		
		$stmt->bindParam(6, $strDateEnd,  PDO::PARAM_STR);		
		$stmt->bindParam(7, $intOffset,   PDO::PARAM_INT);		
		$stmt->bindParam(8, $intLimit,    PDO::PARAM_INT, 6);		
		$stmt->execute();
		$result = $stmt->fetchAll();        //test($result);
		$intTotal = isset($result[0]['foundRows']) ? $result[0]['foundRows'] : 0;
		return isset($result) ? $result : null;
	}
	/**
     * @author: Nguyen Thanh Tung
	 * @description: create new user 
	 * @param interger $intLoginId: id admin loggin (as root id width insert permission)
	 * @param string $strUsername:
	 * @param string $strPassword: encrypt md5
	 * @param string $strEmail
	 * @param string $strFirstname
	 * @param string $strLastname
	 * @param string $strBirthday 
	 * @param integer $intGender 
	 * @param integer $intStatus 
	 * @param integer $intCountry 
	 * @param integer $intRole
	 * @param string $strSrc
	 * @return integer
     *    0: INSERT SUCCESS
     *    1: ERROR - USERNAME EXISTS
     *    2: ERROR - NOT PERMISSION ADD
     *      	
	 */
	public function add($intLoginId, $strUsername, $strPassword, $strEmail, $strFirstname, $strLastname,
					$strBirthday, $intGender, $intStatus, $strCountry, $intRole) {
		$stmt = $this->database->prepare('CALL users_add(?,?,?,?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginId,  PDO::PARAM_INT,11);
		$stmt->bindParam(2, $strUsername, PDO::PARAM_STR, 50);
		$stmt->bindParam(3, $strPassword, PDO::PARAM_STR, 64);
		$stmt->bindParam(4, $strEmail,    PDO::PARAM_STR, 150);
		$stmt->bindParam(5, $strFirstname,PDO::PARAM_STR, 50);		
		$stmt->bindParam(6, $strLastname, PDO::PARAM_STR, 50);
		$stmt->bindParam(7, $strBirthday, PDO::PARAM_STR);
		$stmt->bindParam(8, $intGender,   PDO::PARAM_INT,4);
		$stmt->bindParam(9, $intStatus,   PDO::PARAM_INT,4);
		$stmt->bindParam(10,$strCountry , PDO::PARAM_STR, 2);
		$stmt->bindParam(11,$intRole,     PDO::PARAM_INT,4);	
		
		$stmt->execute();	
        $result = $stmt->fetch();	 
        return isset($result['errCode']) ? $result['errCode'] : -1;	
	}
	/**
     * @author: Nguyen Thanh Tung
	 * @description: update user 
	 * @param interger $intLoginId: id admin loggin (as root id width insert permission)
     * @param integer $intAdminId: ID
	 * @param string $strUsername: Username
	 * @param string $strPassword: encrypt md5
	 * @param string $strFullname:
	 * @param integer $intType: role 
     * @param integer $intRoomId: working room
	 * @return integer
     *    0: INSERT SUCCESS
     *    1: ERROR - NOT PERMISSION UPDATE
     *      	
	 */
	public function update($intLoginId,$intUserId,$strFirstname,$strLastname,$strBirthday,$intGender,$strAdddress,$intDistrict,$intProvince,$strCountry,								$strPassport,$strCard_id ) {
		$stmt = $this->database->prepare('CALL users_update(?,?,?,?,?,?,
                                                             ?,?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginId,    PDO::PARAM_INT);
		$stmt->bindParam(2, $intUserId,     PDO::PARAM_INT);
		$stmt->bindParam(3, $strFirstname,  PDO::PARAM_STR, 50);
		$stmt->bindParam(4, $strLastname,   PDO::PARAM_STR, 50);
		$stmt->bindParam(5, $strBirthday,   PDO::PARAM_STR);
		$stmt->bindParam(6, $intGender,     PDO::PARAM_INT,4);		
		$stmt->bindParam(7, $strAdddress,   PDO::PARAM_STR, 150);		
		$stmt->bindParam(8, $intDistrict,   PDO::PARAM_INT, 11);		
		$stmt->bindParam(9, $intProvince,   PDO::PARAM_INT, 11);		
		$stmt->bindParam(10, $strCountry,   PDO::PARAM_STR, 2);		
		$stmt->bindParam(11, $strPassport,  PDO::PARAM_STR, 20);		
		$stmt->bindParam(12, $strCard_id,   PDO::PARAM_STR, 20);		
		$stmt->execute();
		$result = $stmt->fetch();
		return isset($result['errCode']) ? $result['errCode'] : -1;	
	}
    /**
     * @author: Nguyen Thanh Tung
	 * @description: change password  
	 * @param interger $intLoginId: id admin loggin (as root id width insert permission)
     * @param integer $intUserId: ID
	 * @param string $strPassword: encrypt md5
	 * @return integer
     *    0: INSERT SUCCESS
     *    1: ERROR - NOT PERMISSION UPDATE
     *      	
	 */
	public function updatePassword($intLoginId, $intUserId, $strPassword) {
        $stmt = $this->database->prepare('CALL users_update_password(?,?,?)');
		$stmt->bindParam(1, $intLoginId,    PDO::PARAM_INT);
        $stmt->bindParam(2, $intUserId,    PDO::PARAM_INT);
        $stmt->bindParam(3, $strPassword,   PDO::PARAM_STR, 64);
		$stmt->execute();
        $result = $stmt->fetch(); 
		return isset($result['errCode']) ? $result['errCode'] : -1;
	}	
	/**
     * @author: Nguyen Thanh Tung
	 * @description: change email  
	 * @param interger $intLoginId: id admin loggin (as root id width insert permission)
     * @param integer $intUserId: ID
	 * @param string $strEmail
	 * @return integer
     *    0: INSERT SUCCESS
     *    1: ERROR - NOT PERMISSION UPDATE
     *      	
	 */
	public function updateEmail($intLoginId, $intUserId, $strEmail) {
        $stmt = $this->database->prepare('CALL users_update_email(?,?,?)');
		$stmt->bindParam(1, $intLoginId,  PDO::PARAM_INT);
        $stmt->bindParam(2, $intUserId,   PDO::PARAM_INT);
        $stmt->bindParam(3, $strEmail,    PDO::PARAM_STR, 150);
		$stmt->execute();
        $result = $stmt->fetch(); 
		return isset($result['errCode']) ? $result['errCode'] : -1;
	}
	/**
     * @author: Nguyen Thanh Tung
	 * @description: update status
	 * @param interger $intLoginId: id admin loggin (as root id width insert permission)
     * @param integer $intUserId: ID
	 * @param string $strPassword: encrypt md5
	 * @return integer
     *    0: INSERT SUCCESS
     *    1: ERROR - NOT PERMISSION UPDATE
     *      	
	 */
	public function updateStatus($intLoginId, $intUserid, $intStatus, $intPerm) {
					
		$stmt = $this->database->prepare('call users_update_status(?,?,?,?)');
		$stmt->bindParam(1, $intLoginId, PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $intUserid, PDO::PARAM_INT, 11);
		$stmt->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
		$stmt->bindParam(4, $intPerm, PDO::PARAM_INT, 4);
        $stmt->execute();
		$arrResult 	= $stmt->fetch();
		$intError	= isset($arrResult['errCode']) ? $arrResult['errCode'] : -1;
		if($intError == 0) {
		}
		return $intError;
	}
	/**
	*	@description: get Error code from query result
	*	@param: mixed query result
	*	@return: int
	*/
	public function getErrorCode ( $tableData) {
		return isset($tableData['errCode'])?intval($tableData['errCode']):0;
	}	
	
}